using Assets.IOLibrary;
using Assets.IOLibrary.Global;
using Assets.IOLibrary.Model;
using Assets.Scripts;
using DataSourceLibray;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AlarmBehaviourScript : BaseScripts
{
    // Start is called before the first frame update
    void Start()
    {
        AppSettings.Instance.Alarm.Warning += Alarm_Warning;

        InvokeRepeating(nameof(Warning), 0, 1);
    }


    //private void Alarm_Warning1(string arg1, Dictionary<string, object> arg2)
    //{
    //    var script = this.FindGameObject("PreAlarm").GetComponent<PreAlarm>();
    //    script.ShowAlarm(arg1, "", arg2);
    //}

    private void Alarm_Warning(object sender, DataSourceLibray.Handler.WarningEventArgs e)
    {
        AppSettings.Instance.Sys.IO.PLC101.warmValue = AppSettings.Instance.Io.PLC.PLC101.DI1;
        AppSettings.Instance.Sys.IO.PLC102.warmValue = AppSettings.Instance.Io.PLC.PLC102.DI1;
        AppSettings.Instance.Sys.IO.PLC103.warmValue = AppSettings.Instance.Io.PLC.PLC103.DI1;

        AppSettings.Instance.Sys.IO.PLC201.warmValue = AppSettings.Instance.Io.PLC.PLC201.DI1;
        AppSettings.Instance.Sys.IO.PLC202.warmValue = AppSettings.Instance.Io.PLC.PLC202.DI1;

        //AppSettings.Instance.Sys.IO.PLC10x1.warmValue = AppSettings.Instance.Io.PLC.PLC10x1.DI1;
        //AppSettings.Instance.Sys.IO.PLC10x2.warmValue = AppSettings.Instance.Io.PLC.PLC10x2.DI1;
        //AppSettings.Instance.Sys.IO.PLC10x3.warmValue = AppSettings.Instance.Io.PLC.PLC10x3.DI1;

        //AppSettings.Instance.Sys.IO.PLC20x1.warmValue = AppSettings.Instance.Io.PLC.PLC20x1.DI1;
        //AppSettings.Instance.Sys.IO.PLC20x2.warmValue = AppSettings.Instance.Io.PLC.PLC20x2.DI1;



        var script = this.FindGameObject("PreAlarm").GetComponent<PreAlarm>();
        script.ShowAlarm(e.Msg, e.TypeName, sender);
    }

    void Warning()
    {

        AppSettings.Instance.Alarm.OnWarning(AppSettings.Instance.Sys.IO.PLC101, AppSettings.Instance.Io.PLC.PLC101, (s, i) =>
        {
            return (s.Flag == 2 && s.warmValue == false && i.DI1 == true);

        }, "101����", nameof(Assets.IOLibrary.Model.PLC.PLC101), AppSettings.Instance.Io.PLC.PLC101);
        AppSettings.Instance.Sys.IO.PLC101.warmValue = AppSettings.Instance.Io.PLC.PLC101.DI1;


        //return;

        AppSettings.Instance.Alarm.OnWarning(AppSettings.Instance.Sys.IO.PLC102, AppSettings.Instance.Io.PLC.PLC102, (s, i) =>
        {
            return (s.Flag == 2 && s.warmValue == false && i.DI1 == true);

        }, "102����", nameof(Assets.IOLibrary.Model.PLC.PLC102), AppSettings.Instance.Io.PLC.PLC102);
        AppSettings.Instance.Sys.IO.PLC102.warmValue = AppSettings.Instance.Io.PLC.PLC102.DI1;


        AppSettings.Instance.Alarm.OnWarning(AppSettings.Instance.Sys.IO.PLC103, AppSettings.Instance.Io.PLC.PLC103, (s, i) =>
        {
            return (s.Flag == 2 && s.warmValue == false && i.DI1 == true);

        }, "103����", nameof(Assets.IOLibrary.Model.PLC.PLC103), AppSettings.Instance.Io.PLC.PLC103);
        AppSettings.Instance.Sys.IO.PLC103.warmValue = AppSettings.Instance.Io.PLC.PLC103.DI1;

        AppSettings.Instance.Alarm.OnWarning(AppSettings.Instance.Sys.IO.PLC201, AppSettings.Instance.Io.PLC.PLC201, (s, i) =>
        {
            return (s.Flag == 2 && s.warmValue == false && i.DI1 == true);

        }, "201����", nameof(Assets.IOLibrary.Model.PLC.PLC201), AppSettings.Instance.Io.PLC.PLC201);
        AppSettings.Instance.Sys.IO.PLC201.warmValue = AppSettings.Instance.Io.PLC.PLC201.DI1;

        AppSettings.Instance.Alarm.OnWarning(AppSettings.Instance.Sys.IO.PLC202, AppSettings.Instance.Io.PLC.PLC202, (s, i) =>
        {
            return (s.Flag == 2 && s.warmValue == false && i.DI1 == true);

        }, "202����", nameof(Assets.IOLibrary.Model.PLC.PLC202), AppSettings.Instance.Io.PLC.PLC202);
        AppSettings.Instance.Sys.IO.PLC202.warmValue = AppSettings.Instance.Io.PLC.PLC202.DI1;



    }

}
